var cur={
		action:{
			
		},constant:{
			pageNo:1,
			pageSize:15,
		}
}

;$(function() {
	getList();
	 getDeptList();
	 initUpload("excelFile", ctxPath+"/agency/user/upload");
})
 var tale = new $.tale();
function getList(){
	$.ajax({
    	url: ctxPath+"/agency/user/getPage",
        type: 'POST',
        data: {pageNo:cur.constant.pageNo,pageSize:cur.constant.pageSize,"deptId":$("#dept_select").val(),"remark2":$("#remark").val()},
        success: function(d){
        	 if(d.data!=null){
        		 var html="";
        	$.each(d.data.dataList,function(i,item){
        		html+=toHtml(item);
        	})	
        	$("#list_result").html(html);
        	} 
        getPage(d.data.pages,d.data.total);	 
        }
    });
}

//分页
function getPage(pageCount,totalCount){
	kkpager.total =pageCount ;//总页码
	kkpager.totalRecords = totalCount;//总条数
	kkpager.generPageHtml({
		pno : 0,
		mode : 'click',//默认值是link，可选link或者click
		click : function(n){
			cur.constant.pageNo = n;
			getList();
			this.selectPage(n);
		    return false;
		}
	});
}

 function toHtml(data){
	 var img=ctxPath+'/image/defualt.png';
	 if(data.fileId!=null){
		 img=ctxPath+'/file/showImage/'+data.fileId;
	 }
	var ext = data.userExtendEO;
	var dept = data.deptUser;
	var sex ="";
	var position ="";
	var deptName ="";
	var isParty ="";
	if(ext!=null){
		sex=ext.sex;
		position=ext.position;
		isParty=ext.isParty;
	}
	if(dept!=null){
		deptName=dept.deptName;
	}
	 return '<tr>'
         +'<td>'
         +'<a href="'+ctxPath+'/agency/user/detail/'+data.id+'" style="cursor: pointer;"><img src="'+img+'" width="50px;">&nbsp;&nbsp;&nbsp;&nbsp;'
         +'</a></td>'
         +'<td>'
         +data.nickname
         +'</td>'
         +'<td>'
         +sex
         +'</td>'
         +'<td>'
         +deptName
         +'</td>'
         +'<td>'
         +position
         +'</td>'
         +'<td>'
         +data.phone
         +'</td>'
         +'<td>'
         +isParty
         +'</td>'
         +'<td>'
         +'<a  class="btn btn-primary btn-sm waves-effect waves-light m-b-5" href="'+ctxPath+'/agency/user/edit/'+data.id+'"><i class="fa fa-edit"></i>编辑</a>'
         +'<button type="button" class="btn btn-danger btn-sm waves-effect waves-light m-b-5" onclick="del(' + data.id + ',\'' + data.nickname + '\')"><i class="fa fa-trash-o"></i> 删除</button>'
         +'<a  class="btn btn-info btn-sm waves-effect waves-light m-b-5" href="'+ctxPath+'/agency/user/detail/'+data.id+'"><i class="fa fa-rocket"></i>详情</a>'
         +'</td>'
         +'</tr>';
 }
 
 
 
 function del(id,name){
    tale.alertConfirm({
        title:'确定删除'+name+'吗?',
        then: function () {
            tale.post({
                url : ctxPath+'/agency/user/del',
                data: {id: id},
                success: function (result) {
                    if(result && result.success == true){
                        tale.alertOkAndReload(name+'删除成功');
                    } else {
                        tale.alertError(result.msg || name+'删除失败');
                    }
                }
            });
        }
    });
 }
 
 /**
  * 获取组织机构
  * @returns
  */
 function getDeptList(){
 	$.ajax({
         url: "/agency/dept/getAllList",
         type: 'GET',
         success: function(d){
         	var success = d.success;
         	 if(success==true){
         	if(d.data!=null){
         		var result ='<option value="">请选择</option>';
         		
         	$.each(d.data,function(i,item){
         		result+='<option value="'+item.id+'">'+item.name+'</option>';	
         	})
         	$("#dept_select").html(result);
         	}	 
         	}
         	 
 	}
         })
 }
 
 //导出
 function exportExcel(){
	 window.location.href=ctxPath+"/agency/user/exportExcel?deptId="+$("#dept_select").val()+"&remark2="+$("#remark").val();
 }
 
 //导入弹框
 function importModal(){
	 $("#importModal").modal();
 }
 
 
 
 function initUpload(ctrlName, uploadUrl) {
     var control = $('#' + ctrlName);
     control.fileinput({
         language: 'zh', //设置语言
         uploadUrl: uploadUrl, //上传的地址
         uploadAsync: true, //默认异步上传
         showCaption: true,//是否显示标题
         showPreview : false, //是否显示预览
         dropZoneEnabled: false,//是否显示拖拽区域
         showUpload: true, //是否显示上传按钮
         browseClass: "btn btn-primary", //按钮样式
         allowedFileExtensions: ["xls", "xlsx"], //接收的文件后缀
         maxFileCount: 1,//最大上传文件数限制
//         previewFileIcon: '<i class="glyphicon glyphicon-file"></i>',
         showPreview: true, //是否显示预览
         previewFileIconSettings: {
             'xlsx': '<i class="fa fa-file-excel-o text-success"></i>',
             'xls': '<i class="fa fa-file-excel-o text-success"></i>'
         },
         uploadExtraData: function () {
             var extraValue = "test";
             return {"excelType": extraValue};
         }
     });
 }
 $("#excelFile").on("fileuploaded", function (event, data, previewId, index) {
     console.log(data);
     if(data.response.success == true)
     {
         alert(data.files[index].name + "上传成功!");
     //关闭
         $(".close").click();
     }
     else{
         alert(data.files[index].name + "上传失败!" + data.response.message);
     //重置
     $("#excelFile").fileinput("clear");
     $("#excelFile").fileinput("reset");
     $('#excelFile').fileinput('refresh');
     $('#excelFile').fileinput('enable');
     }
 });